package com.wc.AlgoOJ_train.train2_DP训练.c7_状压DP.麦当劳;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/14 17:12
 * @description http://43.138.190.70:8888/p/DP0604
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010, M = 1 << 7;
    // f[i][j] 表示当到第i个包含当前状态m-1状态为j的最大值
    static int[][] f = new int[N][M];
    static int[] w = new int[N];
    // 满足条件的状态
    static int[] state = new int[M << 1];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) w[i] = sc.nextInt();
        // 寻找满足条件的状态
        for (int i = 0; i < 1 << m; i++) {
            if (check(i))
                state[++state[0]] = i;
        }
        for (int i = 1; i <= n; i++) {
            // 枚举当前的状态
            for (int j = 1; j <= state[0]; j++) {
                // state % (1 << m - 1) 作用是去掉最高位, 保证下一位的状态 state >> 1是前面那个去掉最高位的就方便许多
                if ((state[j] & 1) == 1) {
                    f[i][state[j] % (1 << m - 1)] = Math.max(f[i][state[j] % (1 << m - 1)], f[i - 1][state[j] >> 1] + w[i]);
                } else {
                    f[i][state[j] % (1 << m - 1)] = Math.max(f[i][state[j] % (1 << m - 1)], f[i - 1][state[j] >> 1]);
                }
            }
        }
        int res = 0;
        for (int i = 1; i < 1 << m; i++) res = Math.max(res, f[n][i % (1 << m - 1)]);
        out.println(res);
        out.flush();
    }

    static boolean check(int x) {
        int cnt = 0;
        for (int i = 0; i < m; i++) {
            cnt += x >> i & 1;
        }
        return cnt <= m / 2;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
